package br.com.rsrpp.infrastructure.persistence.hibernate;
	
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import br.com.rsrpp.domain.model.Bairro;
import br.com.rsrpp.domain.model.Cidade;
import br.com.rsrpp.domain.model.UF;
import br.com.rsrpp.domain.model.UP;
import br.com.rsrpp.domain.model.UserRepository;
import br.com.rsrpp.domain.model.Usuario;

@Repository
public class UserRepositoryImpl extends HibernateRepository implements UserRepository {

	public void saveOrUpdate(Usuario user) {
		getSession().saveOrUpdate(user);
	}
	
	public Usuario buscarById(int id) {
		return (Usuario) getSession().get(Usuario.class, id);
	}
	
	@SuppressWarnings({"unchecked","rawtypes"})
	public List<Usuario> buscarByEmail(String email) {
		String query = "from Usuario u where u.email = :email";
		List result = getSession().createQuery(query).setString("email", email).list();
		return result;
	}
	
	@SuppressWarnings("unchecked")
	public List<Usuario> buscarTodos() {
		return getSession().createQuery("from Usuario").list();
	}
	
	@SuppressWarnings("unchecked")
	public List<UF> buscarUF() {
		return getSession().createQuery("from UF").list();
	}
	
	public UF buscarUFById(int id) {
		return (UF) getSession().get(UF.class, id);
	}
	
	@SuppressWarnings("unchecked")
	public List<Cidade> buscarCidades(int id) {
		Query q = getSession().createQuery("from Cidade c where c.uf.id_uf = :id");
		q.setInteger("id", id);
		@SuppressWarnings("rawtypes")
		List result = q.list();
		return result;
	}
	
	public Cidade buscarCidadeById(int id) {
		return (Cidade) getSession().get(Cidade.class, id);
	}
	
	@SuppressWarnings("unchecked")
	public List<Bairro> buscarBairros(int id) {
		Query q = getSession().createQuery("from Bairro b where b.cidade.id_cidade = :id");
		q.setInteger("id", id);
		@SuppressWarnings("rawtypes")
		List result = q.list();
		return result;
	}
	
	public Bairro buscarBairroById(int id) {
		return (Bairro) getSession().get(Bairro.class, id);
	}

	public void saveOrUpdate(UP up) {
		getSession().saveOrUpdate(up);
	}
}
